package com.hci.pwf;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class WeatherContentHandler implements ContentHandler{
	boolean current = false;
	boolean forecast = false;
	
	ForecastInfo fi = new ForecastInfo();
	CurrentWeather cw = new CurrentWeather();
	ForecastWeather fw1 = new ForecastWeather();
	ForecastWeather fw2 = new ForecastWeather();
	ForecastWeather fw3 = new ForecastWeather();
	ForecastWeather fw4 = new ForecastWeather();
	int i=1;

	
	ArrayList<WeatherInfo> list = new ArrayList<WeatherInfo>();
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("forecast_information")){
			list.add(fi);
		}
		if(localName.equals("current_conditions")){
			list.add(cw);
			current = false;
		}
		if(localName.equals("forecast_conditions")){
			switch(i){
			case 1: list.add(fw1); break;
			case 2: list.add(fw2); break;
			case 3: list.add(fw3); break;
			case 4: list.add(fw4); break;
			}
			i++;
			forecast = false;
		}
	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("current_conditions")) current = true;
		if(localName.equals("forecast_conditions"))forecast = true;
		
		if(localName.equals("city")) fi.city=atts.getValue(0);
		if(localName.equals("postal_code")) fi.postal_code=atts.getValue(0);
		if(localName.equals("latitude")) fi.latitude=atts.getValue(0);
		if(localName.equals("longitude")) fi.longitude=atts.getValue(0);
//		if(localName.equals("forecast_date")) fi.forecast_date=atts.getValue(0);
//		if(localName.equals("current_date_time")) fi.current_date_time=atts.getValue(0);
		
		if(current == true){
			if(localName.equals("condition")) cw.condition=atts.getValue(0);
			if(localName.equals("temp_f")) cw.temp_f=atts.getValue(0);
			if(localName.equals("temp_c")) cw.temp_c=atts.getValue(0);
			if(localName.equals("humidity")) cw.humidity=atts.getValue(0);
			if(localName.equals("wind_condition")) cw.wind_condition=atts.getValue(0);
			if(localName.equals("icon")) cw.icon="http://www.google.com"+atts.getValue(0);
		}
		
		if(forecast == true){
			if(i==1){
				if(localName.equals("day_of_week")) fw1.date=atts.getValue(0);
				if(localName.equals("low")) fw1.low_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("high")) fw1.high_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("condition")) fw1.condition=atts.getValue(0);
				if(localName.equals("icon")) fw1.icon="http://www.google.com"+atts.getValue(0);
			}
			if(i==2){
				if(localName.equals("day_of_week")) fw2.date=atts.getValue(0);
				if(localName.equals("low")) fw2.low_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("high")) fw2.high_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("condition")) fw2.condition=atts.getValue(0);
				if(localName.equals("icon")) fw2.icon="http://www.google.com"+atts.getValue(0);
			}
			if(i==3){
				if(localName.equals("day_of_week")) fw3.date=atts.getValue(0);
				if(localName.equals("low")) fw3.low_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("high")) fw3.high_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("condition")) fw3.condition=atts.getValue(0);
				if(localName.equals("icon")) fw3.icon="http://www.google.com"+atts.getValue(0);
			}
			if(i==4){
				if(localName.equals("day_of_week")) fw4.date=atts.getValue(0);
				if(localName.equals("low")) fw4.low_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("high")) fw4.high_temp=Integer.toString(((Integer.parseInt(atts.getValue(0))-32)*5/9));
				if(localName.equals("condition")) fw4.condition=atts.getValue(0);
				if(localName.equals("icon")) fw4.icon="http://www.google.com"+atts.getValue(0);
			}
		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}
	

}
